﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;
using System.IO;
using System.Text.RegularExpressions;
using System;
using System.Security.Cryptography;
using System.Text;

public partial class SignUp : System.Web.UI.Page
{
    string logo="default.jpeg";
    int logodefault = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            
        }
        else
        {
            this.pass.Attributes.Add("value", pass.Text);
            this.repass.Attributes.Add("value", repass.Text);
            
        }
    }   

    //boton submit   
    private string crearXml()
    {
        int tienelogo=0;
        string sxml = "";
        if (!File1.HasFile)
        {
            //////////////////
            if (CheckBox1.Checked) { logo = "~//data/logos//default.jpg"; tienelogo = 1; }
            if (CheckBox2.Checked) { logo = "~//data/logos//default1.png"; tienelogo = 1; }
            if (CheckBox3.Checked) { logo = "~//data/logos//logocasa.jpg"; tienelogo = 1; }

        }
        else {
            logo = "~//data//logos//U"+user.Text+"_"+File1.FileName;
            tienelogo = 1;
        
        }

        if (tienelogo == 1)
        {
            string statePinitial = DB.getStateInitial(cbPState.Text);
            string stateBinitial = DB.getStateInitial(cbBState.Text);


            sxml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?> <datos_registro><user>";
            sxml = sxml + user.Text;
            sxml = sxml + "</user><pass>" + pass.Text;
            sxml = sxml + "</pass><name>" + firstname.Text + "</name>";
            sxml = sxml + "<lastname>" + lastname.Text + "</lastname>";
            sxml = sxml + "<email>" + email.Text + "</email>";
            sxml = sxml + "<comp-name>" + compname.Text + "</comp-name>";
            sxml = sxml + "<title>" + title.Text + "</title>";
            sxml = sxml + "<estado>1</estado>";
            sxml = sxml + "<aplicacionid>1</aplicacionid>";
            sxml = sxml + "<perfilid>1</perfilid>";
            sxml = sxml + "<logo>" + logo + "</logo>";
            sxml = sxml + "<direcciones>";

            //para profile
            sxml = sxml + "<direccion><uidID>0</uidID><add1>" + paddress1.Text + "</add1>";
            sxml = sxml + "<add2>" + paddress2.Text + "</add2>";
            sxml = sxml + "<city>" + pcity.Text + "</city>";
            sxml = sxml + "<state> " + statePinitial + " </state>";
            sxml = sxml + "<zip>" + pcity.Text + "</zip>";
            sxml = sxml + "<office-phone>" + Utility.QuitarCaracteresNoNumericos(officephone.Text) + "</office-phone>";
            sxml = sxml + "<cell-phone>" + Utility.QuitarCaracteresNoNumericos(cellphone.Text) + "</cell-phone>";
            sxml = sxml + "<fax>" + Utility.QuitarCaracteresNoNumericos(Fax.Text) + "</fax>";
            sxml = sxml + "<tipo>1</tipo>";
            sxml = sxml + "<credit_card><tacID>0</tacID>";
            sxml = sxml + "<namecreditcard>" + namecreditcard.Text + "</namecreditcard>";
            sxml = sxml + "<creditcard>" + creditcard.Text + "</creditcard>";
            sxml = sxml + "<expdate>" + ExpDate.Text + "</expdate>";
            sxml = sxml + "<ccv>" + cvv.Text + "</ccv>";
            sxml = sxml + "</credit_card></direccion>";

            //Dir Billing

            sxml = sxml + "<direccion><uidID>0</uidID><add1>" + baddress1.Text + "</add1>";
            sxml = sxml + "<add2>" + baddress2.Text + "</add2>";
            sxml = sxml + "<city>" + bcity.Text + "</city>";
            sxml = sxml + "<state>" + statePinitial + "</state>";
            sxml = sxml + "<zip>" + bzip.Text + "</zip>";
            sxml = sxml + "<office-phone></office-phone>";
            sxml = sxml + "<cell-phone></cell-phone>";
            sxml = sxml + "<fax></fax>";
            sxml = sxml + "<tipo>2</tipo>";
            sxml = sxml + "<credit_card><tacID>0</tacID>";
            sxml = sxml + "<namecreditcard>" + namecreditcard.Text + "</namecreditcard>";
            sxml = sxml + "<creditcard>" + creditcard.Text + "</creditcard>";
            sxml = sxml + "<expdate>" + ExpDate.Text + "</expdate>";
            sxml = sxml + "<ccv>" + cvv.Text + "</ccv>";
            sxml = sxml + "</credit_card></direccion>";
            sxml = sxml + "</direcciones>";
            sxml = sxml + "</datos_registro>";
        }
        else {
            Response.Write("<" + "script language='javascript'>");
            Response.Write("alert('Select a default or personal logo');");
            Response.Write("<" + "/script>");
        
        }
        return sxml;
    }
    
   
   
    
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        
        
        try{
        if (validar()==1)
        {
            string sxml = crearXml();
            //PEDIR\consumir webservice enviando sxml
            com.bpophotos.www.Service registrar = new com.bpophotos.www.Service();
          string datos= registrar.RegistraUsuario(sxml);
      
                       
           
           /* String datos = "<?xml version=\"1.0\" encoding=\"utf-8\"?><datos_respuesta>  <respuestaid> 1</respuestaid>";
            datos = datos + "<usuarioid>23</usuarioid><nombre>astrid</nombre> <apellido>bolanos</apellido> <alias>lucianab</alias>";
            datos = datos + " <perfilid>admin</perfilid><permiso>1111111</permiso><imagen>.//data/logos//default.jpeg</imagen></datos_respuesta>";
            * */
            rUser rreg = Utility.getRUser(datos);
            if (rreg != null)
            {
                if (rreg.getRespuestaId() == 1)
                {

                    guardarlogo(rreg);

                    //enviar mail
                    string from = "newclient@bpophoto.com";
                    string to = email.Text;
                    string subject = "Welcome to bpophotos.com";
                    string body = "<div style=\"color:Blue\">   Welcome to <img src=\"http://www.bpophotos.com/img/bpophotoorange.png \" alt=\"\" width=\"200\"></div>";
                    body = body + " Thank you for signing up with BPOPhotos.com ";
                    body = body + " you're just one click away from working with ";
                    body = body + "our profesional staff";
                    // body = body + "<br />Please click <a href=\"www.gmail.com" + rreg.getUsuarioId() + " \"> here </a> to activate your account and start taking advantage of our services";
                    body = body + "<br />Please click <a href=\"http://www.bpophotos.com/useractivate.aspx?id=" + rreg.getUsuarioId() + " \"> here </a> to activate your account and start taking advantage of our services";
                    string result = Utility.sendMail(from, to, subject, body);
                    if (result.Equals("ok"))
                    {
                        Response.Redirect("CRegistro.aspx");
                    }
                    Response.Redirect("CRegistro.aspx");

                }


            }
            else {
                Response.Write("<" + "script language='javascript'>");
                Response.Write("alert('Sorry. SignUp Failed');");
                Response.Write("<" + "/script>");
            
            }
           
            


        }
        }
        catch (Exception ee)
        {

            Utility.writeError(ee.Message + ee.StackTrace);
        }
    }

    private int validar()
    {
        int result = 1;
        if (File1.PostedFile != null) { logodefault = 4; }
        if (email.Equals("")) { result = result * 0; }
        if (cbBState.SelectedIndex == 0) { result = result * 0; }
        if (cbPState.SelectedIndex == 0) { result = result * 0; }
        if (pass.Equals(repass.Text)) { result = result * 0; }
        if (logodefault==0 || logodefault>4){result=result*0;}
        return result;
        
    }

    private void guardarlogo(rUser rreg)
    {
       
        if (File1.PostedFile != null)
        {
            try
            {
                
                
                string filepath = File1.FileName;
                                            
                File1.PostedFile.SaveAs(Server.MapPath("~//data/logos//U"+rreg.getUsuarioId()+"_"+filepath) );
                logo = ".//data/logos//U" + rreg.getUsuarioId() + "_" + filepath;
               //update logo for user WS

            }
            catch (Exception ex)
            {
                Span3.InnerHtml = "Error saving file <b>"+File1.PostedFile.FileName;
                  
            }
        }
       
    }
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (CheckBox1.Checked==true){
            CheckBox2.Checked = false;
            CheckBox3.Checked = false;
            logodefault = 1;
        }
    }
    protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
    {
        if (CheckBox2.Checked == true)
        {
            CheckBox1.Checked = false;
            CheckBox3.Checked = false;
            logodefault = 2;
        }
    }
    protected void CheckBox3_CheckedChanged(object sender, EventArgs e)
    {
        if (CheckBox3.Checked == true)
        {
            CheckBox1.Checked = false;
            CheckBox2.Checked = false;
            logodefault = 3;
        }
    }
    protected void CheckBox4_CheckedChanged(object sender, EventArgs e)
    {
        if (CheckBox4.Checked == true)
        {
            baddress1.Text = paddress1.Text;
            baddress2.Text = paddress2.Text;
            bcity.Text = pcity.Text;
            cbBState.SelectedValue = cbPState.SelectedValue;
            bzip.Text = pzip.Text;
        }
        else {
            baddress1.Text = "";
            baddress2.Text = "";
            bcity.Text = "";
            cbBState.SelectedIndex=0;
            bzip.Text = "";
        
        }
    }
    protected void CheckBox5_CheckedChanged(object sender, EventArgs e)
    {
        logodefault = 4;
    }

    protected void office1_TextChanged(object sender, EventArgs e)
    {

        if (officephone.Text.Length == 10)
        {
            string toffice;


            toffice = "(" + officephone.Text[0].ToString() + officephone.Text[1].ToString() + officephone.Text[2].ToString() + ")"
                + officephone.Text[3].ToString() + officephone.Text[4].ToString() + officephone.Text[5].ToString() + "-"
                + officephone.Text[6].ToString() + officephone.Text[7].ToString() + officephone.Text[8].ToString() + officephone.Text[9].ToString();
            officephone.Text = toffice;
            RegularExpressionValidator6.Enabled = false;

        }
        else
        {
            if (officephone.Text.Length != 13)
            {
                RegularExpressionValidator6.Enabled = true;

            }

        }






    }
    protected void cellphone_textchanged(object sender, EventArgs e)
    {
        if (cellphone.Text.Length == 10)
        {
            string toffice;



            toffice = "(" + cellphone.Text[0].ToString() + cellphone.Text[1].ToString() + cellphone.Text[2].ToString() + ")"
                + cellphone.Text[3].ToString() + cellphone.Text[4].ToString() + cellphone.Text[5].ToString() + "-"
                + cellphone.Text[6].ToString() + cellphone.Text[7].ToString() + cellphone.Text[8].ToString() + cellphone.Text[9].ToString();
            cellphone.Text = toffice;

            RegularExpressionValidator7.Enabled = false;

        }
        else
        {
            if (cellphone.Text.Length != 13)
            {
                RegularExpressionValidator7.Enabled = true;

            }
        }
    }
    protected void faxphone_textchanged(object sender, EventArgs e)
    {

        if (
            Fax.Text.Length == 10

                   )
        {
            string toffice;
            toffice = "(" + Fax.Text[0].ToString() + Fax.Text[1].ToString() + Fax.Text[2].ToString() + ")"
                + Fax.Text[3].ToString() + Fax.Text[4].ToString() + Fax.Text[5].ToString() + "-"
                + Fax.Text[6].ToString() + Fax.Text[7].ToString() + Fax.Text[8].ToString() + Fax.Text[9].ToString();
            Fax.Text = toffice;
            RegularExpressionValidator8.Enabled = false;

        }
        else
        {
            if (Fax.Text.Length != 13)
            {
                RegularExpressionValidator8.Enabled = true;

            }

        }
    }

}
